#ifndef _PAGE_HOME_HPP
#define _PAGE_HOME_HPP

extern void showPage(int pageNum);

// 发送消息label
static lv_label_t	*sendLabel;
// 接收消息
static lv_obj_t		*recMsgBody;
static lv_label_t	*recTimeLabel;
static lv_label_t	*recUserLabel;
static lv_label_t	*recMsgLabel;
// 主体部分尺寸
static int pageHomeWidth	= 300;
static int pageHomeHeight	= 220;
// 输入部分高度
static int pageHomeInputHeight = 35;

static char *unitCode[64] = {0};

// static void pageHomeRotA() {

// }
// static void pageHomeRotB() {

// }
// static void pageHomeRotC() {

// }
// static void pageHomeRotSet() {
	
// }

void pageHomeShowFun() {

}

void pageHomeInputQTask() {
	char *showCode = lv_mem_alloc(1024);
	lv_memset(showCode, 0, 1024);
	char *c;
	while(1) {
		xQueueReceive(inputQ, &c, portMAX_DELAY);
		if(stGroup[ST_TRAN_CODE].intValue) {
			if(strcmp(c, " ") != 0) {
				strcat(unitCode, c);
			}
			else {
				if(strcmp(unitCode, "") != 0) {
					LVMT;
					lv_label_ins_text(sendLabel, LV_LABEL_POS_LAST, tranCode(unitCode));
					LVMG;
				}
				strcpy(unitCode, "");
			}
		}
		LVMT;
		lv_label_ins_text(sendLabel, LV_LABEL_POS_LAST, c);
		strcat(showCode, c);
		lv_label_set_text(sendLabel, showCode);
		LVMG;
	}
	vTaskDelete(NULL);
}

void pageHomeWsTask() {
	BaseType_t bits;
	char *rMsg;
	while(1) {
		bits = xQueueReceive(wsDataQ, &rMsg, portMAX_DELAY);
		if(bits == pdPASS) {
			char *result = NULL;
			result = strtok(rMsg, "|");
			for(int i = 0; i < 3; i ++) {
				if(i == 0) {
					// recTime
					LVMT;
					lv_label_set_text(recTimeLabel, result);
					LVMG;
				}
				else if(i == 1) {
					// fromUser
					LVMT;
					lv_label_set_text(recUserLabel, result);
					LVMG;
				}
				else if(i == 2) {
					// msg
					if(stGroup[ST_TRAN_CODE].intValue) {
						char *tranStr = NULL;
						tranStr = tranCodeStr(result);
						LVMT;
						lv_label_set_text(recMsgLabel, tranStr);
						LVMG;
						playCode(tranStr);
						free(tranStr);
					}
					else {
						LVMT;
						lv_label_set_text(recMsgLabel, result);
						LVMG;
						playCode(result);
					}
										
				}
				result = strtok(NULL, "|");
			}
			free(rMsg);
		}
	}
	vTaskDelete(NULL);
}


static void pageHomeInit() {
	LVMT;
	// 接收消息
	lv_obj_t *recBody = lv_obj_create(pageHome);
	lv_obj_remove_style_all(recBody);
	lv_obj_set_style_radius(recBody, 8, 0);
	lv_obj_set_style_pad_all(recBody, 0, 0);
	lv_obj_set_size(recBody, pageHomeWidth, pageHomeHeight - pageHomeInputHeight - 20);
	lv_obj_set_style_bg_opa(recBody, LV_OPA_COVER, 0);
	lv_obj_set_style_bg_color(recBody, LV_COLOR_WHITE, 0);
	lv_obj_set_scrollbar_mode(recBody, LV_SCROLLBAR_MODE_OFF);
	lv_obj_align(recBody, LV_ALIGN_TOP_MID, 0, 10);	

	// 顶部信息外框
	lv_obj_t *recTopBody = lv_obj_create(recBody);
	lv_obj_remove_style_all(recTopBody);
	lv_obj_set_size(recTopBody, pageHomeWidth, 20);
	lv_obj_set_style_pad_hor(recTopBody, 5, 0);
	lv_obj_set_style_border_side(recTopBody, LV_BORDER_SIDE_BOTTOM, 0);
	lv_obj_set_style_border_width(recTopBody, 1, 0);
	lv_obj_set_style_border_color(recTopBody, LV_COLOR_MAIN_BG_2, 0);
	lv_obj_set_scrollbar_mode(recTopBody, LV_SCROLLBAR_MODE_OFF);


	// 时间
	recTimeLabel = lv_label_create(recTopBody);
	lv_obj_set_size(recTimeLabel, 60, 20);
	lv_label_set_long_mode(recTimeLabel, LV_LABEL_LONG_CLIP);
	lv_obj_set_style_text_color(recTimeLabel, LV_COLOR_FONT_1, 0);
	lv_label_set_text(recTimeLabel, "");
	lv_obj_set_style_text_align(recTimeLabel, LV_TEXT_ALIGN_RIGHT, 0);
	lv_obj_align(recTimeLabel, LV_ALIGN_RIGHT_MID, 0, 2);

	// 呼号
	recUserLabel = lv_label_create(recTopBody);
	lv_obj_set_style_text_color(recUserLabel, LV_COLOR_FONT_1, 0);
	lv_label_set_long_mode(recUserLabel, LV_LABEL_LONG_CLIP);
	lv_obj_set_size(recUserLabel, pageHomeWidth - 100, 20);
	lv_label_set_text(recUserLabel, "");
	lv_obj_set_style_text_align(recUserLabel, LV_TEXT_ALIGN_LEFT, 0);
	lv_obj_align(recUserLabel, LV_ALIGN_LEFT_MID, 0, 2);

	// 收到的信息
	recMsgBody = lv_obj_create(recBody);
	lv_obj_remove_style_all(recMsgBody);
	lv_obj_set_style_text_color(recMsgBody, LV_COLOR_FONT_2, 0);
	lv_obj_set_size(recMsgBody, pageHomeWidth, pageHomeHeight - pageHomeInputHeight - 50);
	lv_obj_align(recMsgBody, LV_ALIGN_TOP_MID, 0, 22);
	lv_obj_set_style_pad_all(recMsgBody, 0, 0);
	lv_obj_set_style_radius(recMsgBody, 0, 0);
	lv_obj_set_style_border_width(recMsgBody, 0, 0);
	lv_obj_set_style_bg_opa(recMsgBody, LV_OPA_COVER, 0);
	

	recMsgLabel = lv_label_create(recMsgBody);
	lv_obj_remove_style_all(recMsgLabel);
	lv_obj_set_size(recMsgLabel, pageHomeWidth - 12, LV_SIZE_CONTENT);
	lv_obj_set_align(recMsgLabel, LV_ALIGN_TOP_MID);
	lv_obj_set_style_text_font(recMsgLabel, CFONT, 0);
	lv_obj_set_style_text_color(recMsgLabel, LV_COLOR_FONT_1, 0);
	lv_label_set_long_mode(recMsgLabel, LV_LABEL_LONG_WRAP);
	lv_label_set_text(recMsgLabel, "");

	// 发送消息
	lv_obj_t *sendBody = lv_obj_create(pageHome);
	lv_obj_remove_style_all(sendBody);
	lv_obj_set_style_radius(sendBody, pageHomeInputHeight, 0);
	lv_obj_set_style_pad_all(sendBody, 0, 0);
	lv_obj_set_style_bg_opa(sendBody, LV_OPA_COVER, 0);
	lv_obj_set_size(sendBody, pageHomeWidth, pageHomeInputHeight - 10);
	lv_obj_align(sendBody, LV_ALIGN_TOP_MID, 0, pageHomeHeight - pageHomeInputHeight - 2 );
	lv_obj_set_style_bg_color(sendBody, LV_COLOR_WHITE, 0);
	lv_obj_set_scrollbar_mode(sendBody, LV_SCROLLBAR_MODE_OFF);


	sendLabel = lv_label_create(sendBody);
	lv_obj_set_width(sendLabel, pageHomeWidth - 30);
	lv_label_set_long_mode(sendLabel, LV_LABEL_LONG_CLIP);
	lv_obj_set_style_text_align(sendLabel, LV_TEXT_ALIGN_RIGHT, 0);
	lv_label_set_text(sendLabel, "");
	lv_obj_set_style_text_font(sendLabel, CFONT, 0);
	lv_obj_align(sendLabel, LV_ALIGN_TOP_MID, 0, 4);

	
	LVMG;

	// 显示收到的电码任务
	xTaskCreatePinnedToCore(pageHomeWsTask, "pageHomeWsTask", MEM_UNIT * 4, NULL, 0, NULL, 1);
	// 显示输入的电码
	// xTaskCreatePinnedToCore(pageHomeInputQTask, "pageHomeInputQTask", MEM_UNIT * 4, NULL, 0, NULL, 1);
}


void pageHomeScroll(int op, uint32_t num) {
	// if(!scrolling) {
	// 	int toPos = 0;
	// 	// 向上滚动
	// 	if(op == 0)
	// 	{
	// 		toPos = uiMsgContScrollPos - CONFIG_SCROLL_STEP;
	// 		if(toPos <= 0)
	// 		{
	// 			toPos = 0;
	// 		}
	// 	}
	// 	// 向下滚动
	// 	else if(op == 1)
	// 	{
	// 		toPos = uiMsgContScrollPos + CONFIG_SCROLL_STEP;
	// 		int bottom = lv_obj_get_scroll_bottom(recMsgBody);
	// 		printf("bottom = %d\n", bottom);
	// 		if(bottom <= CONFIG_SCROLL_STEP)
	// 		{
	// 			toPos = uiMsgContScrollPos + bottom;
	// 		}
	// 		else
	// 		{
	// 			toPos = uiMsgContScrollPos + CONFIG_SCROLL_STEP;
	// 		}
	// 	}
	// 	uiMsgContScrollPos = toPos;
	// 	lv_obj_scroll_to_y(recMsgBody, toPos, CONFIG_SCROLL_ANIM);
	// }
}


// 更新输入内容
void pageHomeAddInput(char *c) {
	if(stGroup[ST_TRAN_CODE].intValue) {
		if(strcmp(c, " ") != 0) {
			strcat(unitCode, c);
		}
		else {
			if(strcmp(unitCode, "") != 0) {
				LVMT;
				lv_label_ins_text(sendLabel, LV_LABEL_POS_LAST, tranCode(unitCode));
				LVMG;
			}
			strcpy(unitCode, "");
		}
	}
	LVMT;
	lv_label_ins_text(sendLabel, LV_LABEL_POS_LAST, c);
	LVMG;
}

// 清楚输入内容
void pageHomeCleanInput() {
	LVMT;
	lv_label_set_text(sendLabel, "");
	LVMG;
}

#endif